﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MineSweeperChallenge
{
    /// <summary>
    /// Interaction logic for StepByStepPlayWindow.xaml
    /// </summary>
    public partial class StepByStepPlayWindow : Window, IGameRunner
    {
        private Grid[][] mVisualPlayingField;

        public StepByStepPlayWindow()
        {
            InitializeComponent();
        }

        public void RunGame(ISweeper pSweeper, int pTimes, int pWidth, int pHeight, int pMines, Action<string> pCallbackAction, ProgressBar pProgressBar)
        {
            for(int i = 0 ; i < pWidth; i++)
            {
                grdPlayingField.ColumnDefinitions.Add(new ColumnDefinition());
            }
            for (int i = 0; i < pHeight; i++)
            {
                grdPlayingField.RowDefinitions.Add(new RowDefinition());
            }
            Game lGame = new Game(pSweeper, pWidth, pHeight, pMines);
            lGame.Initialize();
            RefreshPlayingField(lGame.mPlayingMineField);
            btnNextMove.Click += (sender, e) =>
                {
                    Game.Result lResult = lGame.PlayStep();
                    RefreshPlayingField(lGame.mPlayingMineField);
                    if (lResult != null || lGame.mPlayingMineField.SelectMany(pArray => pArray).Count(pValue => pValue == -2) == 1)
                    {
                        btnNextMove.IsEnabled = false;
                        if (pCallbackAction != null) pCallbackAction(null);
                    }
                };

            tblFooterText.Text = string.Format("Playing a game with {0} mines using sweeper \"{1}\" ({2}, by {3})", pMines, pSweeper.Name, pSweeper.Version, pSweeper.Creator);

            this.ShowDialog();
        }

        private void RefreshPlayingField(int[][] pPlayingMineField)
        {
            if (mVisualPlayingField == null)
            {
                mVisualPlayingField = new Grid[pPlayingMineField.Length][];
                for (int i = 0; i < pPlayingMineField.Length; i++)
                {
                    mVisualPlayingField[i] = new Grid[pPlayingMineField[i].Length];
                    for (int j = 0; j < pPlayingMineField[i].Length; j++)
                    {
                        mVisualPlayingField[i][j] = new Grid() { Background = new SolidColorBrush(Colors.Blue) };
                        Border lFieldBorder = new Border()
                            {
                                Child = mVisualPlayingField[i][j],
                                BorderBrush = new SolidColorBrush(Colors.Black),
                                BorderThickness = new Thickness(1)
                            };
                        grdPlayingField.Children.Add(lFieldBorder);
                        Grid.SetColumn(lFieldBorder, i);
                        Grid.SetRow(lFieldBorder, j);
                    }
                }
            }
            else
            {
                for (int i = 0; i < pPlayingMineField.Length; i++)
                {
                    for (int j = 0; j < pPlayingMineField[i].Length; j++)
                    {
                        if (((SolidColorBrush)mVisualPlayingField[i][j].Background).Color == Colors.Blue)
                        {
                            switch (pPlayingMineField[i][j])
                            {
                                case -2: break;
                                case -1: mVisualPlayingField[i][j].Background = new SolidColorBrush(Colors.Red); break;
                                default: mVisualPlayingField[i][j].Background = new SolidColorBrush(Colors.White);
                                    mVisualPlayingField[i][j].Children.Add(new TextBlock()
                                    {
                                        Text = Convert.ToString(pPlayingMineField[i][j]),
                                        HorizontalAlignment = HorizontalAlignment.Center,
                                        VerticalAlignment = VerticalAlignment.Center
                                    }); break;
                            }
                        }
                    }
                }
            }
        }
    }
}
